//
//  MSAreaPickerView.m
//  MuRenHe
//
//  Created by mls on 2021/5/17.
//

#import "MSAreaPickerView.h"

@interface MSAreaPickerView ()
<
UIPickerViewDataSource,
UIPickerViewDelegate
>

@property (nonatomic, strong) NSArray<MSAreaModel *>    *provinceArr;
@property (nonatomic, strong) NSArray<MSAreaModel *>    *cityArr;
@property (nonatomic, strong) NSArray<MSAreaModel *>    *areaArr;
@property (nonatomic, strong) UIPickerView              *pickerView;
@property (nonatomic, assign) NSInteger                 row1;
@property (nonatomic, assign) NSInteger                 row2;
@property (nonatomic, assign) NSInteger                 row3;

@end

@implementation MSAreaPickerView


#pragma mark - event
/** 取消 */
- (void)clickCancelBtn
{
    if ([self.delegate respondsToSelector:@selector(ms_areaPcikerViewDidCancel:)])
    {
        [self.delegate ms_areaPcikerViewDidCancel:self];
    }
}
/** 完成 */
- (void)clickRightBtn
{
    if ([self.delegate respondsToSelector:@selector(ms_areaPickerViewDidComplete:province:city:area:)])
    {
        [self.delegate ms_areaPickerViewDidComplete:self
                                           province:self.provinceArr[self.row1]
                                               city:self.cityArr[self.row2]
                                               area:self.areaArr[self.row3]];
    }
}


#pragma mark - public method

- (void)ms_reloadData
{
    self.hidden = NO;
    [self.pickerView reloadAllComponents];
    [self.pickerView selectRow:self.row1
                   inComponent:0
                      animated:YES];
    [self.pickerView selectRow:self.row2
                   inComponent:0
                      animated:YES];
    [self.pickerView selectRow:self.row3
                   inComponent:0
                      animated:YES];
}


#pragma mark - UIPickerViewDataSource
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0)
    {
        return self.provinceArr.count;
    }
    else if (component == 1)
    {
        return self.cityArr.count;
    }
    else
    {
        return self.areaArr.count;
    }
}

#pragma mark - UIPickerViewDelegate

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return MS_SCREEN_WIDTH / 3.0;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 88 * MS_ADAPTATION_RATIO;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view
{
    MSAreaModel *area;
    if (component == 0)
    {
        area = self.provinceArr[row];
    }
    else if (component == 1)
    {
        area = self.cityArr[row];
    }
    else
    {
        area = self.areaArr[row];
    }
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,
                                                               0,
                                                               MS_SCREEN_WIDTH / 3.0,
                                                               88 * MS_ADAPTATION_RATIO)];
    label.font = MS_SYSTEM_FONT_OF_SIZE(13);
    label.textColor = MS_HEX_COLOR(0x4B4B4D);
    label.textAlignment = NSTextAlignmentCenter;
    label.text = area.name;
    
    return label;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0)
    {
        self.row1 = row;
        self.cityArr = self.provinceArr[self.row1].children;
        self.row2 = 0;
        [self.pickerView selectRow:self.row2
                       inComponent:1
                          animated:YES];
        [self.pickerView reloadComponent:1];
        
        self.areaArr = self.cityArr[self.row2].children;
        self.row3 = 0;
        [self.pickerView selectRow:self.row3
                       inComponent:2
                          animated:YES];
        [self.pickerView reloadComponent:2];
    }
    else if (component == 1)
    {
        self.row2 = row;
        self.areaArr = self.cityArr[self.row2].children;
        self.row3 = 0;
        [self.pickerView selectRow:self.row3
                       inComponent:2
                          animated:YES];
        [self.pickerView reloadComponent:2];
    }
    else
    {
        self.row3 = row;
    }
}


#pragma mark - init

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.row1 = 0;
        self.row2 = 0;
        self.row3 = 0;
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.65];
        [self addSubview:self.pickerView];
    }
    return self;
}


#pragma mark - lazy loading

- (UIPickerView *)pickerView
{
    if (!_pickerView)
    {
        _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,
                                                                     MS_SCREEN_HEIGHT - 415 *  MS_ADAPTATION_RATIO - MS_SAFE_AREA_BOTTOM_HEIGHT,
                                                                     MS_SCREEN_WIDTH,
                                                                     415 * MS_ADAPTATION_RATIO)];
        _pickerView.backgroundColor = UIColor.whiteColor;
        _pickerView.dataSource = self;
        _pickerView.delegate = self;
        _pickerView.showsSelectionIndicator = YES;
        
        UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,
                                                                         MS_SCREEN_HEIGHT - 415 *  MS_ADAPTATION_RATIO - MS_SAFE_AREA_BOTTOM_HEIGHT - 44,
                                                                         MS_SCREEN_WIDTH,
                                                                         44)];
        
        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"取消"
                                                                     style:UIBarButtonItemStyleDone
                                                                    target:self
                                                                    action:@selector(clickCancelBtn)];
        leftItem.tintColor = MS_HEX_COLOR(0x3076F4);//设置取消按钮字体颜色
        //空白
        UIBarButtonItem *btn4 =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                             target:self
                                                                             action:nil];
        
        UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"完成"
                                                                      style:UIBarButtonItemStyleDone
                                                                     target:self action:@selector(clickRightBtn)];
        rightItem.tintColor = MS_HEX_COLOR(0x3076F4);//设置确定按钮字体颜色
        toolBar.items = @[leftItem,
                          btn4,
                          rightItem];
        [self addSubview:toolBar];
    }
    
    return _pickerView;
}

- (NSArray<MSAreaModel *> *)provinceArr
{
    if (!_provinceArr)
    {
//        NSArray *arr = [self ms_JSONObjectForResource:@"area"
//                                               ofType:@"json"];
//        _provinceArr = [NSArray yy_modelArrayWithClass:[MSAreaModel class] json:arr];;
    }
    
    return _provinceArr;
}

- (NSArray<MSAreaModel *> *)cityArr
{
    if (!_cityArr)
    {
        _cityArr = self.provinceArr[0].children;
    }
    
    return _cityArr;
}

- (NSArray<MSAreaModel *> *)areaArr
{
    if (!_areaArr)
    {
        _areaArr = self.cityArr[0].children;
    }
    
    return _areaArr;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

@implementation MSAreaModel

+ (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper
{
    return @{@"Id" : @"id"};
}

+ (nullable NSDictionary<NSString *, id> *)modelContainerPropertyGenericClass
{
    return @{@"children" : @"MSAreaModel"};
}

@end
